using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WpfApp1.Services;
using WpfApp1.ViewModels;
using WpfApp1.Models;

namespace WpfApp1.Views;

public partial class AdminDashboard : Window
{
    private readonly AdminDashboardViewModel _viewModel;
    private Student? SelectedStudent => StudentDataGrid.SelectedItem as Student;
    
    public AdminDashboard(IStudentService studentService, ICourseService courseService, ICourseSelectService courseSelectService)
    {
        InitializeComponent();
        _viewModel = new AdminDashboardViewModel(studentService, courseService, courseSelectService);
        DataContext = _viewModel;
        // 自动加载所有学生数据
        _viewModel.SearchStudent(string.Empty);
        // 自动加载所有课程数据
        _viewModel.SearchCourse(string.Empty);
        //自动加载所有选课数据
        _viewModel.SearchAllSelections();
    }
    // 学生管理
    private void SearchStudent_Click(object sender, RoutedEventArgs e)
    {
        _viewModel.SearchStudent(StudentNumberTextBox.Text);
    }

    // 新增学生
    private void AddStudent_Click(object sender, RoutedEventArgs e)
    {
        if (SelectedStudent != null)
        {
            MessageBox.Show("请先取消选中行再进行新增操作。");
            return;
        }

        // 验证必填字段
        if (string.IsNullOrWhiteSpace(StudentNameInput.Text) ||
            string.IsNullOrWhiteSpace(StudentNumberInput.Text) ||
            string.IsNullOrWhiteSpace(AgeInput.Text) ||
            string.IsNullOrWhiteSpace(DepartmentInput.Text) ||
            GenderComboBox.SelectedIndex == -1)
        {
            MessageBox.Show("请填写所有必填字段！");
            return;
        }

        // 验证年龄格式
        if (!int.TryParse(AgeInput.Text, out int age))
        {
            MessageBox.Show("请输入有效的年龄！");
            return;
        }

        var student = new Student(
            0,
            StudentNameInput.Text,
            StudentNumberInput.Text,
            GenderComboBox.SelectedIndex,
            age,
            DepartmentInput.Text
        );
        
        _viewModel.AddStudent(student);
        ClearStudentForm();
    }

    // 编辑学生
    private void EditStudent_Click(object sender, RoutedEventArgs e)
    {
        if (StudentDataGrid.SelectedItem is Student selectedStudent)
        {
            // 检查关键字段是否被修改
            if (selectedStudent.StudentNumber != StudentNumberInput.Text ||
                selectedStudent.Name != StudentNameInput.Text)
            {
                MessageBox.Show("学号和姓名不允许修改！");
                // 恢复原值
                StudentNumberInput.Text = selectedStudent.StudentNumber;
                StudentNameInput.Text = selectedStudent.Name;
                return;
            }
            selectedStudent.Name = StudentNameInput.Text;
            selectedStudent.Gender = GenderComboBox.SelectedIndex;
            selectedStudent.Age = int.Parse(AgeInput.Text);
            selectedStudent.Department = DepartmentInput.Text;
            
            _viewModel.EditStudent(selectedStudent);
            ClearStudentForm();
        }
    }

    // 删除学生
    private void DeleteStudent_Click(object sender, RoutedEventArgs e)
    {
        if (StudentDataGrid.SelectedItem is Student selectedStudent)
        {
            _viewModel.DeleteStudent(selectedStudent);
            ClearStudentForm();
        }
    }

    //检测选中学生
    private void StudentDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (StudentDataGrid.SelectedItem is Student student)
        {
            StudentNumberInput.Text = student.StudentNumber;
            StudentNameInput.Text = student.Name;
            GenderComboBox.SelectedIndex = student.Gender;
            AgeInput.Text = student.Age.ToString();
            DepartmentInput.Text = student.Department;
        }
    }

    // 清空学生表单
    private void ClearStudentForm()
    {
        StudentNumberInput.Text = string.Empty;
        StudentNameInput.Text = string.Empty;
        GenderComboBox.SelectedIndex = -1;
        AgeInput.Text = string.Empty;
        DepartmentInput.Text = string.Empty;
    }

    // 课程管理模块
    private void SearchCourse_Click(object sender, RoutedEventArgs e)
    {
        _viewModel.SearchCourse(CourseNumberTextBox.Text);
    }
    private void AddCourse_Click(object sender, RoutedEventArgs e)
    {
        if (string.IsNullOrEmpty(CourseNumberInput.Text) || 
            string.IsNullOrEmpty(CourseNameInput.Text) || 
            string.IsNullOrEmpty(CreditsInput.Text) || 
            string.IsNullOrEmpty(HoursInput.Text))
        {
            MessageBox.Show("请填写所有必要信息！");
            return;
        }
        var course = new Course(
            0,  // course_id 由数据库自动生成
            CourseNumberInput.Text,  // course_number
            CourseNameInput.Text,    // course_name
            int.Parse(CreditsInput.Text),  // credits
            int.Parse(HoursInput.Text),    // hours
            TeacherNumberInput.Text  // teacher_number
        );
        
        _viewModel.AddCourse(course);
        ClearCourseForm();
    }
    private void EditCourse_Click(object sender, RoutedEventArgs e)
    {
        if (CourseDataGrid.SelectedItem is Course course)
        {
            if (course.CourseNumber != CourseNumberInput.Text)
            {
                MessageBox.Show("课程号不允许修改！");
                CourseNumberInput.Text = course.CourseNumber;
                return;
            }
            course.CourseName = CourseNameInput.Text;
            course.Credits = int.Parse(CreditsInput.Text);
            course.Hours = int.Parse(HoursInput.Text);
            course.TeacherNumber = TeacherNumberInput.Text;
            _viewModel.UpdateCourse(course);
            ClearCourseForm();
        }
        
    }

    // 删除课程
    private void DeleteCourse_Click(object sender, RoutedEventArgs e)
    {
        if (CourseDataGrid.SelectedItem is Course course)
        {
            if (MessageBox.Show("确定要删除这门课程吗？", "确认删除", 
                MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
            {
                _viewModel.DeleteCourse(course);
                ClearCourseForm();
            }
        }
    }

    //检测选中课程
    private void CourseDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (CourseDataGrid.SelectedItem is Course course)
        {
            CourseNumberInput.Text = course.CourseNumber;
            CourseNameInput.Text = course.CourseName;
            CreditsInput.Text = course.Credits.ToString();
            HoursInput.Text = course.Hours.ToString();
            TeacherNumberInput.Text = course.TeacherNumber;
        }
    }

    // 清空课程表单
    private void ClearCourseForm()
    {
        CourseNumberInput.Text = string.Empty;
        CourseNameInput.Text = string.Empty;
        CreditsInput.Text = string.Empty;
        HoursInput.Text = string.Empty;
        TeacherNumberInput.Text = string.Empty;
    }
    //选课管理
    private void SearchAllSelections_Click(object sender, RoutedEventArgs e)
    {
        _viewModel.SearchAllSelections();
    }

    //搜索选课(按学号)
    private void SearchSelectionByStudent_Click(object sender, RoutedEventArgs e)
    {
        if (string.IsNullOrEmpty(SelectionStudentNumberTextBox.Text))
        {
            MessageBox.Show("请输入学号！");
            return;
        }
        _viewModel.SearchSelectionByStudent(SelectionStudentNumberTextBox.Text);
    }

    //搜索选课(按课程号)
    private void SearchSelectionByCourse_Click(object sender, RoutedEventArgs e)
    {
        if (string.IsNullOrEmpty(SelectionCourseNumberTextBox.Text))
        {
            MessageBox.Show("请输入课程号！");
            return;
        }
        _viewModel.SearchSelectionByCourse(SelectionCourseNumberTextBox.Text);
    }

    //关闭窗口
    private void CloseButton_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
    }

    //窗口移动
    private void Window_MouseDown(object sender, MouseButtonEventArgs e)
    {
        if (e.ChangedButton == MouseButton.Left)
        {
            this.DragMove();
        }
    }
    
    //取消选中学生
    private void ClearSelection_Click(object sender, RoutedEventArgs e)
    {
        StudentDataGrid.SelectedItem = null;
        ClearStudentForm();
    }

    //取消选中课程
    private void ClearCourseSelection_Click(object sender, RoutedEventArgs e)
    {
        CourseDataGrid.SelectedItem = null;
        ClearCourseForm();
    }

}
    
